﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            CountVisistor();
            FillReport();
        }
    }
    void CountVisistor()
    {
        lblVisistorCount.Text = Application["visistor"].ToString();
        lblTime.Text = DateTime.Now.ToLongDateString();
        lblTotalNews.Text = DatabaseInteraction.CountNews().ToString();
    }
    void FillReport()
    {
        int newsID = Convert.ToInt32(Request.QueryString["newsID"]);
        New_Report n = DatabaseInteraction.GetNewsByID(newsID);
        lblWorldTitle.Text = n.TitleNews;
        lblNewWorldContent.Text = n.NewsContent;
        lblTimePost.Text = n.TimePost.ToString();
        lblAuthor.Text = n.Author;
        lblComments.Text = n.CountComments.ToString();
        lblViews.Text = n.CountRead.ToString();
        FillComments(newsID);
    }
    void FillComments(int newID)
    {
        pnlWorldComments.Controls.Clear();
        List<Comment> list = DatabaseInteraction.GetComment(newID);
        if (list.Count > 0)
        {
            foreach (Comment c in list)
            {                
                MyControls_UserComments comment = LoadControl("~/MyControls/UserComments.ascx") as MyControls_UserComments;
                comment.Comment.Text = c.CommentContent;
                comment.Author.Text = c.Author;
                comment.Time.Text = c.TimePost.ToString();
                pnlWorldComments.Controls.Add(comment);
            }
        }
    }
    void PostComment()
    {        
        int newsID = Convert.ToInt32(Request.QueryString["newsID"]);
        Comment c = new Comment()
        {
            NewsID = newsID,
            CommentContent = txtCommentsContent.Text,
            Author = txtUserCommentName.Text,
            TimePost = DateTime.Now
        };
        DatabaseInteraction.AddComment(c);
        FillComments(newsID);
    }
}